﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InfoLayer;
using ControlLayer;

namespace GUI
{
    public partial class frmBaoCaoMatDo : Form
    {
        private BaoCaoMatDoControl baoCaoMatDoControl;
        private BaoCaoMatDoInfo baoCaoMatDoInfo;
        private PhongControl phongControl;
        private ChiTietBaoCaoMatDoControl chiTietBaoCaoMatDoControl;
        private ChiTietBaoCaoMatDoInfo chiTietBaoCaoMatDoInfo;

        public frmBaoCaoMatDo()
        {
            InitializeComponent();
            baoCaoMatDoControl = new BaoCaoMatDoControl();
            baoCaoMatDoInfo = new BaoCaoMatDoInfo();
            phongControl = new PhongControl();
            chiTietBaoCaoMatDoControl = new ChiTietBaoCaoMatDoControl();
            chiTietBaoCaoMatDoInfo = new ChiTietBaoCaoMatDoInfo();
        }

        private void btnLapBaoCao_Click(object sender, EventArgs e)
        {
            if (comboBoxThangLapBaoCao.SelectedItem.ToString() == "")
            {
                MessageBox.Show("Vui lòng nhập tháng báo cáo!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int TongSoNgayThue = 0;

            //Lay Tong doanh thu
            DataTable dt;
            dt = phongControl.LayBangDanhSachPhong();
            int rows = dt.Rows.Count;
            for (int i = 0; i < rows; i++)
            {
                DataTable _dt;
                _dt = baoCaoMatDoControl.LayBaoCaoMatDo_MaPhong(dt.Rows[i][0].ToString(), Convert.ToInt32(comboBoxThangLapBaoCao.SelectedItem.ToString()));
                int _rows = _dt.Rows.Count;
                for (int j = 0; j < _rows; j++)
                {
                    TongSoNgayThue += Convert.ToInt32(_dt.Rows[j][2].ToString());
                }
            }
            //MessageBox.Show("Tong doanh thu " + TongSoNgayThue, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //Them Bang Bao cao doanh thu
            dt = baoCaoMatDoControl.LayBangDanhSachBaoCao();
            if (dt.Rows.Count > 0)
                baoCaoMatDoInfo.MaBaoCaoMatDo = baoCaoMatDoControl.TaoMaTuDong(dt.Rows[dt.Rows.Count - 1][0].ToString(), 5);
            else
                baoCaoMatDoInfo.MaBaoCaoMatDo = baoCaoMatDoControl.TaoMaTuDong("MBCMD000", 5);
            baoCaoMatDoInfo.ThangBaoCao = Convert.ToInt32(comboBoxThangLapBaoCao.SelectedItem.ToString());

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (Convert.ToInt32(dt.Rows[i][1].ToString()) == baoCaoMatDoInfo.ThangBaoCao)
                {
                    MessageBox.Show("Tháng " + baoCaoMatDoInfo.ThangBaoCao + " đã được lập! ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }

            //Them Bang Chi tiet bao cao doanh thu
            dt = phongControl.LayBangDanhSachPhong();
            for (int i = 0; i < rows; i++)
            {
                int songaythue_Phong = 0;
                String tenPhong = "";
                tenPhong = dt.Rows[i][1].ToString();
                DataTable _dt;
                _dt = baoCaoMatDoControl.LayBaoCaoMatDo_MaPhong(dt.Rows[i][0].ToString(), Convert.ToInt32(comboBoxThangLapBaoCao.SelectedItem.ToString()));
                int _rows = _dt.Rows.Count;
                for (int j = 0; j < _rows; j++)
                {
                    songaythue_Phong += Convert.ToInt32(_dt.Rows[j][2].ToString());
                    
                }

                DataTable dt_CT;
                dt_CT = chiTietBaoCaoMatDoControl.LayBangDanhSachChiTietBaoCao();
                if (dt_CT.Rows.Count > 0)
                    chiTietBaoCaoMatDoInfo.MaChiTietBaoCaoMatDo = chiTietBaoCaoMatDoControl.TaoMaTuDong(dt_CT.Rows[dt_CT.Rows.Count - 1][0].ToString(), 7);
                else
                    chiTietBaoCaoMatDoInfo.MaChiTietBaoCaoMatDo = chiTietBaoCaoMatDoControl.TaoMaTuDong("MCTBCMD000", 7);
                chiTietBaoCaoMatDoInfo.MaBaoCaoMatDo = baoCaoMatDoInfo.MaBaoCaoMatDo;
                chiTietBaoCaoMatDoInfo.SoNgayThue = songaythue_Phong;
                if (TongSoNgayThue == 0)
                    chiTietBaoCaoMatDoInfo.TyLe = 0;
                else
                    chiTietBaoCaoMatDoInfo.TyLe = (float)songaythue_Phong / (float)TongSoNgayThue;
                chiTietBaoCaoMatDoInfo.MaPhong = dt.Rows[i][0].ToString();

                chiTietBaoCaoMatDoControl.Insert(ref chiTietBaoCaoMatDoInfo);

                //Hien thi len DataGriView
                String[] dong = new string[] { (i + 1).ToString(), tenPhong, chiTietBaoCaoMatDoInfo.SoNgayThue.ToString(), (chiTietBaoCaoMatDoInfo.TyLe * 100).ToString() + "%" };
                dataGridViewChiTietBaoCao.Rows.Add(dong);
            }

            textBoxTongSoNgayThue.Text = TongSoNgayThue.ToString();

            baoCaoMatDoControl.Insert(ref baoCaoMatDoInfo);
            MessageBox.Show("Thêm thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

            btnLapBaoCao.Enabled = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnInBaoCao_Click(object sender, EventArgs e)
        {
            rptBaoCaoMatDo rpt = new rptBaoCaoMatDo();

            CrystalDecisions.Shared.TableLogOnInfo logOnInfoTable = new CrystalDecisions.Shared.TableLogOnInfo();
            foreach (CrystalDecisions.CrystalReports.Engine.Table currentTable in rpt.Database.Tables)
            {
                logOnInfoTable.ConnectionInfo.ServerName = Application.StartupPath + "\\QLKS.mdb";
                currentTable.ApplyLogOnInfo(logOnInfoTable);
            }

            frmBaoCaoMatDo_CrystalReport frm = new frmBaoCaoMatDo_CrystalReport();
            frm.crystalReportViewerBaoCaoMatDo.ReportSource = rpt;
            frm.Show();
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            dataGridViewChiTietBaoCao.Rows.Clear();
            textBoxTongSoNgayThue.Clear();
            btnLapBaoCao.Enabled = true;
        }
    }
}
